var StageManager = function () { }
StageManager.prototype.test = function () { },
    StageManager.prototype.loadStage = function (stageIndex) {
        window.GameViewData.curStage = window.GameViewData.stageArr[stageIndex];

        // 将地图对象加到布局中
        window.GameViewData.mapLayout.node.removeAllChildren();
        for (var i = 0; i < window.GameViewData.curStage.landPropertiesArr.length; i++) {
            var tmpLand = cc.instantiate(window.GameViewData.landNode);
            var tmpNode = cc.instantiate(window.GameViewData.landNode.node);
            tmpLand.state = window.GameViewData.curStage.landPropertiesArr[i].state;
            if (window.GameViewData.curStage.landPropertiesArr[i].type == 1) {
                tmpLand.type = window.GameViewData.curStage.landPropertiesArr[i].type;
                tmpNode = cc.instantiate(window.GameViewData.pointLandNode.node);
                // } else if (window.GameViewData.curStage.landPropertiesArr[i].type == 16) {
            } else if (window.GameViewData.curStage.landPropertiesArr[i].type == 16) {

                tmpLand.type = window.GameViewData.curStage.landPropertiesArr[i].type;
                tmpNode = cc.instantiate(window.GameViewData.blankLandNode.node);
            }
            tmpLand.node = tmpNode;
            window.GameViewData.curStage.landArr.push(tmpLand);
            window.GameViewData.mapLayout.node.addChild(tmpNode);
        }

        // 修改布局大小
        window.GameViewData.mapLayout.node.width = window.GameViewData.landNode.node.width * window.GameViewData.curStage.rowNum
            + window.GameViewData.mapLayout.paddingLeft + window.GameViewData.mapLayout.paddingRight
            + window.GameViewData.mapLayout.spacingX * window.GameViewData.curStage.rowNum;


        window.GameViewData.mapLayout.node.height = window.GameViewData.landNode.node.height * (window.GameViewData.curStage.allNum / window.GameViewData.curStage.rowNum)
            + window.GameViewData.mapLayout.paddingTop + window.GameViewData.mapLayout.paddingBottom
            + window.GameViewData.mapLayout.spacingY * (window.GameViewData.curStage.allNum / window.GameViewData.curStage.rowNum) ;

        window.GameViewData.mapLayout.updateLayout();
        window.BizFunc.resetRobotByStageConfig();
        window.BizFunc.resetDynamicData();
    },
    StageManager.prototype.createTestData = function () {

        console.log(window.cfg);
        // 创建关卡1
        var stage = new window.Stage(1, 2);
        var landArr = new Array();
        var landPropertiesArr = new Array();

        var rowNum = 7;
        var allNum = 66;
        var targetIndex1 = 1;
        var targetIndex2 = 3;
        var initLocalX = 1;
        var initLocalY = 0;
        var initDir = 12001;

        for (var i = 0; i < allNum; i++) {
            var obj = {};
            obj.type = 0;
            obj.state = 0;
            if (i == targetIndex1 || i == targetIndex2) {
                obj.type = 1;
            }
            landPropertiesArr.push(obj);
        }

        stage.landArr = landArr;
        stage.landPropertiesArr = landPropertiesArr;
        stage.rowNum = rowNum;
        stage.allNum = allNum;
        stage.initLocalX = initLocalX;
        stage.initLocalY = initLocalY;
        stage.initDir = initDir;

        // 创建关卡2
        var stage2 = new window.Stage(1, 2);
        var landArr2 = new Array();
        var landPropertiesArr2 = new Array();

        var rowNum2 = 7;
        var allNum2 = 33;
        var targetIndex12 = 6;
        var targetIndex22 = 8;
        var initLocalX2 = 4;
        var initLocalY2 = 2;
        var initLocalY2 = 2;
        var initDir2 = 12002;

        for (var i = 0; i < allNum2; i++) {
            var obj = {};
            obj.type = 0;
            obj.state = 0;
            if (i == targetIndex12 || i == targetIndex22) {
                obj.type = 1;
            }
            landPropertiesArr2.push(obj);
        }
        stage2.landArr = landArr2;
        stage2.landPropertiesArr = landPropertiesArr2;
        stage2.rowNum = rowNum2;
        stage2.allNum = allNum2;
        stage2.initLocalX = initLocalX2;
        stage2.initLocalY = initLocalY2;
        stage2.initDir = initDir2;

        window.GameViewData.stageArr.push(stage);
        window.GameViewData.stageArr.push(stage2);

        // console.log(window.cfg);
        // window.GameViewData.stageArr.push(window.cfg.StageCfgData);

        var cfgArr = window.cfg.StageCfgData.getStageCfgData();
        var landCfgArr = window.cfg.LandCfgData.getLandCfgData();

        for (var i = 0; i < cfgArr.keys.length; i++) {
            var cfg = cfgArr.get(cfgArr.keys[i]);
            var stage = new window.Stage();
            stage.setValueFromCfg(cfg);

            for (var j = 0; j < landCfgArr.keys.length; j++) {
                var landCfg = landCfgArr.get(landCfgArr.keys[j]);
                if (cfg.id == landCfg.stageId) {
                    stage.landPropertiesArr.push(landCfg);
                    if (landCfg.type == 1) {
                        stage.target++;
                    }

                }
            }
            window.GameViewData.stageArr.push(stage);
        }
        console.log(window.GameViewData.stageArr);

    }

module.exports = StageManager; 